/*
PROG: baying  
LANG: C++     
ID: therellub1
*/

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

//==========VARIABLES==========
priority_queue< long long, vector<long long>, greater<long long> > v;
int N;
long long c, a1, b1, d1, a2, b2, d2;

void inp()
{
    cin >> c >> N;    
    cin >> a1 >> b1 >> d1;
    cin >> a2 >> b2 >> d2;
}

long long f1(long long c)
{
    return a1*c/d1 + b1;
}

long long f2(long long c)
{
    return a2*c/d2 + b2;
}

void process()
{
    v.push(c);
    int count = 0;
    long long prex = 0;
    while (!v.empty())
    {
        long long x = v.top();
        v.pop();
        if (x == prex) continue;
        prex = x;
        
        count++;
        if (count == N) 
        {
            cout << x << endl;
            break;
        }
    
        v.push(f1(x));
        v.push(f2(x));
    }
}
 
int main()
{
    freopen("baying.in", "r", stdin);
    freopen("baying.out", "w", stdout);
    
    inp();
    process();
    
    getchar();
          
    return 0;
}
